2 + 2[1] 4
10 - 3[1] 7
5 * 4[1] 20
20 / 5[1] 4
sqrt(16)[1] 4
Applied Methods · MA Public Administration
At the end of Session 1, you should be able to:
<-library()View(), head(), str(), and summary()R is the language and statistical software that performs the calculations.
RStudio is an integrated development environment that makes R easier to use. In practice, we open R through RStudio.
Recommended installation order:
| Pane | Use |
|---|---|
| Source | Write and save scripts or Quarto documents |
| Console | Run R commands interactively |
| Environment | See objects currently stored in memory |
| History | See commands previously executed |
| Files | Navigate folders and files |
| Plots | Display figures |
| Packages | Install, update, and load packages |
| Help | Read documentation |
A good habit is to write code in the Source pane and run it from there. This makes your work reproducible.
R can be used as a calculator.
2 + 2[1] 4
10 - 3[1] 7
5 * 4[1] 20
20 / 5[1] 4
sqrt(16)[1] 4
Parentheses matter.
10 + 5 * 2[1] 20
(10 + 5) * 2[1] 30
Use <- to store a value in an object.
age <- 24
city <- "Geneva"
share <- 37.5Read this as:
agegets the value24.
Once an object exists, you can reuse it.
age + 1[1] 25
paste("The city is", city)[1] "The city is Geneva"
Recommended object names:
mean_income <- 5200
number_students <- 28
public_staff_2026 <- 455Avoid names with spaces, accents, or special symbols.
mean income <- 5200 # bad: contains a space
mean-income <- 5200 # bad: R reads - as subtraction
économie <- 5200 # avoid accents in object namesone_number <- 10
one_text <- "public administration"
one_logical <- TRUE
one_factor <- factor("Geneva")
class(one_number)[1] "numeric"
class(one_text)[1] "character"
class(one_logical)[1] "logical"
class(one_factor)[1] "factor"
Common classes:
| Class | Meaning | Example |
|---|---|---|
| numeric | numbers | 10, 3.14 |
| character | text | "Geneva" |
| logical | TRUE/FALSE | TRUE, FALSE |
| factor | categorical variable | factor("urban") |
A vector contains several values.
communes <- c("Carouge", "Cologny", "Lancy", "Meyrin")
population <- c(22600, 5700, 34400, 27000)Useful functions:
length(population)[1] 4
mean(population)[1] 22425
min(population)[1] 5700
max(population)[1] 34400
Extract elements with square brackets.
communes[1][1] "Carouge"
population[1:2][1] 22600 5700
In R, missing values are coded as NA.
x <- c(1, 2, 3, NA)
mean(x)[1] NA
mean(x, na.rm = TRUE)[1] 2
Many functions have an na.rm = TRUE argument, which means “remove missing values before computing the result”.
A data frame is a rectangular dataset.
municipal_data <- data.frame(
commune = communes,
population = population
)
municipal_data commune population
1 Carouge 22600
2 Cologny 5700
3 Lancy 34400
4 Meyrin 27000
Inspect a data frame:
head(municipal_data) commune population
1 Carouge 22600
2 Cologny 5700
3 Lancy 34400
4 Meyrin 27000
str(municipal_data)'data.frame': 4 obs. of 2 variables:
$ commune : chr "Carouge" "Cologny" "Lancy" "Meyrin"
$ population: num 22600 5700 34400 27000
summary(municipal_data) commune population
Length:4 Min. : 5700
Class :character 1st Qu.:18375
Mode :character Median :24800
Mean :22425
3rd Qu.:28850
Max. :34400
nrow(municipal_data)[1] 4
ncol(municipal_data)[1] 2
Create a new variable with $.
municipal_data$population_thousands <- municipal_data$population / 1000
municipal_data commune population population_thousands
1 Carouge 22600 22.6
2 Cologny 5700 5.7
3 Lancy 34400 34.4
4 Meyrin 27000 27.0
A function takes input, performs operations, and returns output.
divide_by_4_add_1 <- function(x) {
x / 4 + 1
}
divide_by_4_add_1(20)[1] 6
The same function can often be applied to a vector.
divide_by_4_add_1(c(4, 8, 12))[1] 2 3 4
A package extends what R can do.
Install a package once:
install.packages("readr")Load it each time you open a new R session and need it:
library(readr)Important distinction:
install.packages() downloads and installs the package on your computer.library() loads the package into the current R session.Different commands exist to inspect the data:
View(first_data)
head(first_data)
str(first_data)
summary(first_data)Use ? before a function name.
?mean
?read.csv
?data.frameOr use the Help pane in RStudio.
When you get an error:
library().Example:
my_budgte + 10The problem is a spelling error: my_budgte does not exist.
For every assignment and group project, try to follow this logic:
# 1. Load packages
# 2. Import data
# 3. Inspect data
# 4. Clean or transform data
# 5. Analyze data
# 6. Produce tables or figures
# 7. Interpret the resultThe objective is not only to obtain a result, but to make the result reproducible and understandable.